$(document).ready(function() {
  // echo
  util.echo();
  // img delay
  $.each($('.img-delay'), (index, item) => {
    const $item = $(item);
    const src = $item.data('src');
    const width = $item.data('width');
    const height = $item.data('height');
    $item.attr('src', util.combineImageUrl(src, width, height));
    $(item).removeClass('img-delay');
  });
});

$(document).on('echo-ready', function() {
  // hotload file
  if (env.site.hotloadFile && !util.user.op.anonymous) {
    let __hotloadFileTime = 0;
    window.setTimeout(() => {
      const subscribePath = `/a/cms/hotloadFile/${env.site.hotloadFile}`;
      util.io.subscribe(subscribePath, () => {
        location.reload(true);
      }, () => {
        util.performAction({
          method: 'post',
          url: '/a/cms/site/checkFile',
          body: { file: env.site.hotloadFile, mtime: __hotloadFileTime },
        }).then(data => {
          if (!__hotloadFileTime) {
            if (data && data.mtime) {
              __hotloadFileTime = data.mtime;
            }
          } else {
            if (data && data.mtime !== __hotloadFileTime) {
              location.reload(true);
            }
          }
        });
      });
    }, 0);
  }
});
